package cc.autoapi.pucong.liteflow.design.mobal;

import cc.autoapi.pucong.liteflow.design.core.exception.DesignException;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @author pucong
 */
public enum NodeCodeEnum {

    common("common"),
    SWITCH("switch"),
    IF("if"),
    FOR("for"),
    WHILE("while"),
    ITERATOR("iterator"),
    BOOLEAN("boolean"),
    BREAK("break"),
    SCRIPT("script"),
    SWITCH_SCRIPT("switchScript"),
    BOOLEAN_SCRIPT("booleanScript"),
    FOR_SCRIPT("forScript"),
    CATCH("catch"),
    CATCH_END("catchEnd"),
    PROCESS("process");

    private String key;

    NodeCodeEnum(String key) {
        this.key = key;
    }

    public static NodeCodeEnum findType(NodeTypeEnum type) {
        if (type == NodeTypeEnum.FOR) {
            return NodeCodeEnum.FOR;
        } else if (type == NodeTypeEnum.COMMON) {
            return NodeCodeEnum.common;
        } else if (type == NodeTypeEnum.SWITCH) {
            return NodeCodeEnum.SWITCH;
        } else if (type == NodeTypeEnum.BOOLEAN) {
            return NodeCodeEnum.BOOLEAN;
        } else if (type == NodeTypeEnum.ITERATOR) {
            return NodeCodeEnum.ITERATOR;
        } else if (type == NodeTypeEnum.SCRIPT) {
            return NodeCodeEnum.SCRIPT;
        } else if (type == NodeTypeEnum.SWITCH_SCRIPT) {
            return NodeCodeEnum.SWITCH_SCRIPT;
        } else if (type == NodeTypeEnum.BOOLEAN_SCRIPT) {
            return NodeCodeEnum.BOOLEAN_SCRIPT;
        } else if (type == NodeTypeEnum.FOR_SCRIPT) {
            return NodeCodeEnum.FOR_SCRIPT;
        } else {
            throw new DesignException("error node type，not support");
        }
    }

    /**
     * 字典code
     *
     * @return
     */
    public String getValue() {
        return this.key;
    }

    /**
     * 字典名称
     *
     * @return
     */
    public String getName() {
        return this.name();
    }

    public static NodeCodeEnum getItemByValue(String value) {
        NodeCodeEnum[] itemArr = NodeCodeEnum.values();
        if (StringUtils.isNotBlank(value)) {
            for (NodeCodeEnum item : itemArr) {
                if (value.equals(item.getValue())) {
                    return item;
                }
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return key;
    }
}
